﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OUSMS.AppLogic.BusinessLogicLayer;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.Helpers;
using System.Web.ModelBinding;
namespace OUSMS
{
    public partial class StudyAreasList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // 返回类型可以更改为 IEnumerable，但是为了支持
        // 分页和排序，必须添加以下参数:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<OUSMS.AppLogic.DataAccessLayer.StudyAreas> StudyAreasGridView_GetData(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            StudyAreasBLL studyAreasBLL = new StudyAreasBLL();
            return studyAreasBLL.GetAllActiveStudyAreas(maximumRows, startRowIndex, out totalRowCount, sortByExpression);
        }        

        protected void StudyAreasGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label indexLbl = e.Row.FindControl("indexLbl") as Label;
                indexLbl.Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void addStudyAreaBtn_Click(object sender, EventArgs e)
        {
            try
            {
                StudyAreas studyArea = new StudyAreas();
                studyArea.Name = studyAreaNameTxtBox.Text.Trim();
                studyArea.IsDeleted = false;
                StudyAreasBLL studyAreaBLL = new StudyAreasBLL();
                studyAreaBLL.AddStudyArea(studyArea);
                msgLbl.Text = studyArea.Name + "已添加成功。";

                StudyAreasGridView.DataBind();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                msgLbl.Text = string.Format("添加错误:{0}", ex.Message);
            }
        }

        protected void deleteLnkBtn_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton deleteLnkBtn = sender as LinkButton;
                StudyAreasBLL studyAreaBLL = new StudyAreasBLL();
                StudyAreas studyArea2Delete = studyAreaBLL.GetStudyAreaByID(Convert.ToInt32(deleteLnkBtn.CommandArgument));
                studyArea2Delete.IsDeleted = true;
                studyAreaBLL.SaveChanges();

                StudyAreasGridView.DataBind();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                msgLbl.Text = string.Format("记录删除失败，失败原因:{0}", ex.Message);
            }
        }
    }
}